package com.yixing.tech.common.utils;

import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

   public static  String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static  String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String YYYYMMDD = "yyyyMMdd";

    public synchronized static String getDateStr(SimpleDateFormat dateFormat) {
        return getDateStr(dateFormat, new Date());
    }

    public synchronized static String getDateStr(SimpleDateFormat dateFormat, Date date) {
        return dateFormat.format(date);
    }

    public static Date praseUtcDate(String date) throws ParseException {
        Date parse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(date);
        return parse;
    }

    public static Date praseDate(SimpleDateFormat dateFormat, String date) {
        Date parse = null;
        try {
            parse = dateFormat.parse(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static Date praseDate(String date, SimpleDateFormat... dateFormat) {
        Date parse = null;
        for (SimpleDateFormat sdf : dateFormat) {
            try {
                parse = sdf.parse(date);
                break;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return parse;
    }

    public synchronized static String praseDate4(String date) {
        return getDateStr(new SimpleDateFormat(
                "yyyy-MM-dd"), praseDate(new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"), date));
    }

    public static Date addDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, day);// 24小时制
        return cal.getTime();
    }

    public static Date addMin(Date date, int min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, min);// 24小时制
        return cal.getTime();
    }

    public static Date addHour(Date date, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        return cal.getTime();
    }

    public static Date addMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);// 24小时制
        return cal.getTime();
    }


    public static Date getBeginDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        System.out.println(dayofweek);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return cal.getTime();
    }

    /**
     * 给时间加上几个小时
     *
     * @param day  当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return 字符串类型时间
     */
    public static String addDateMinut(String day, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);
        date = cal.getTime();
        return format.format(date);

    }

    public static String getMinute(LocalDateTime inDate, LocalDateTime nowDate) {
        Duration duration = Duration.between(inDate, nowDate);
        long hour = duration.getSeconds() / ChronoUnit.HOURS.getDuration().getSeconds();
        long minute = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) / ChronoUnit.MINUTES.getDuration().getSeconds();
        long second = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) - minute * ChronoUnit.MINUTES.getDuration().getSeconds();
        return hour + "小时" + minute + "分钟" + second + "秒";
    }

    /**
     * 通过秒毫秒数判断两个时间的间隔的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }


    //Date转换为LocalDateTime
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //LocalDateTime转换为Date
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 毫秒转时间数量
     *
     * @param ms
     * @return
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        sb.append((minute > 0 ? minute : 1) + "分");

        return sb.toString();
    }

    /**
     * 根据时间戳获取整时的时间戳，注意：纳秒不考虑
     * 1697162462000(2023-10-13 10:01:02) -> 1697162400000(2023-10-13 10:00:00)
     *
     * @param sourceMill 源
     * @param after      true：往后取整小时时间戳；false：往前取整小时时间戳
     */
    public static Long limitHour(Long sourceMill, boolean after) {
        Assert.notNull(sourceMill, "时间戳不能为null");
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(sourceMill), ZoneOffset.systemDefault());
        localDateTime = localDateTime.withNano(0);
        int minute = localDateTime.getMinute();
        int second = localDateTime.getSecond();
        if (minute != 0 || second != 0) {
            if (after) {
                localDateTime = localDateTime.plusHours(1).withMinute(0).withSecond(0);
            } else {
                localDateTime = localDateTime.withMinute(0).withSecond(0);
            }
        }
        return localDateTime.atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
    }


}
